package com.cmbird.utils;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * @author zyj
 * @date 2023-01-12 17:14
 */
public class DownloadUtils {

    /**
     * 下载更新文件
     * @param urlStr
     * @return
     * @throws MalformedURLException
     */
    public static String downloadNet(String urlStr) throws MalformedURLException {
        // 下载网络文件
        int bytesum = 0;
        int byteread = 0;
        String fileName = urlStr.substring(urlStr.lastIndexOf("/") + 1);
        URL url = new URL(urlStr);
        try {
            URLConnection conn = url.openConnection();
            InputStream inStream = conn.getInputStream();
            FileOutputStream fs = new FileOutputStream(fileName);
            byte[] buffer = new byte[1204];
            int length;
            while ((byteread = inStream.read(buffer)) != -1) {
                bytesum += byteread;
                fs.write(buffer, 0, byteread);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileName;
    }

    public static void main(String[] args) {
        String s = "http://192.168.0.7:9000/app/PeiFangPrintModule.zip";
        System.out.println(s.lastIndexOf("/"));
    }

}
